{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6d6f5cec-636b-4688-bdec-44e83a2f98d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "import networkx as nx\n",
    "from scipy.ndimage import zoom\n",
    "def load_and_validate_data(file_path):\n",
    "    \"\"\"\n",
    "    Load data from a file and return it.\n",
    "    Return None if data is missing.\n",
    "    \"\"\"\n",
    "    try:\n",
    "        loaded_data = np.load(file_path)\n",
    "        \n",
    "        ppg_f = loaded_data.get('ppg_f')\n",
    "        ecg_f = loaded_data.get('ecg_f')\n",
    "        seg_dbp = loaded_data.get('seg_dbp')\n",
    "        seg_sbp = loaded_data.get('seg_sbp')\n",
    "        \n",
    "        \n",
    "        if ppg_f is None or ecg_f is None or seg_dbp is None or seg_sbp is None:\n",
    "            return None\n",
    "\n",
    "        return ppg_f, ecg_f, seg_dbp, seg_sbp\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(f\"Error loading {file_path}: {e}\")\n",
    "        return None\n",
    "\n",
    "def combine_data_from_folder(folder_path, batch_size=100):\n",
    "    \"\"\"\n",
    "    Combine data from all valid files in the folder in batches.\n",
    "    \"\"\"\n",
    "    combined_ppg = []\n",
    "    combined_ecg = []\n",
    "    combined_seg_dbp = []\n",
    "    combined_seg_sbp = []\n",
    "\n",
    "    for file_name in os.listdir(folder_path):\n",
    "        file_path = os.path.join(folder_path, file_name)\n",
    "        \n",
    "        if not file_path.endswith('.npz'):\n",
    "            continue\n",
    "        \n",
    "        data = load_and_validate_data(file_path)\n",
    "        \n",
    "        if data is None:\n",
    "            print(f\"Skipping invalid file: {file_path}\")\n",
    "            continue\n",
    "        \n",
    "        ppg_f, ecg_f, seg_dbp, seg_sbp = data\n",
    "        \n",
    "        combined_ppg.append(ppg_f)\n",
    "        combined_ecg.append(ecg_f)\n",
    "        combined_seg_dbp.append(seg_dbp)\n",
    "        combined_seg_sbp.append(seg_sbp)\n",
    "        \n",
    "        if len(combined_ppg) >= batch_size:\n",
    "            combined_ppg = np.concatenate(combined_ppg, axis=0)\n",
    "            combined_ecg = np.concatenate(combined_ecg, axis=0)\n",
    "            combined_seg_dbp = np.concatenate(combined_seg_dbp, axis=0)\n",
    "            combined_seg_sbp = np.concatenate(combined_seg_sbp, axis=0)\n",
    "            \n",
    "            yield combined_ppg, combined_ecg, combined_seg_dbp, combined_seg_sbp\n",
    "            \n",
    "            combined_ppg = []\n",
    "            combined_ecg = []\n",
    "            combined_seg_dbp = []\n",
    "            combined_seg_sbp = []\n",
    "            \n",
    "\n",
    "    if combined_ppg:\n",
    "        combined_ppg = np.concatenate(combined_ppg, axis=0)\n",
    "    else:\n",
    "        combined_ppg = np.array([])\n",
    "        \n",
    "    if combined_ecg:\n",
    "        combined_ecg = np.concatenate(combined_ecg, axis=0)\n",
    "    else:\n",
    "        combined_ecg = np.array([])\n",
    "        \n",
    "    if combined_seg_dbp:\n",
    "        combined_seg_dbp = np.concatenate(combined_seg_dbp, axis=0)\n",
    "    else:\n",
    "        combined_seg_dbp = np.array([])\n",
    "        \n",
    "    if combined_seg_sbp:\n",
    "        combined_seg_sbp = np.concatenate(combined_seg_sbp, axis=0)\n",
    "    else:\n",
    "        combined_seg_sbp = np.array([])\n",
    "\n",
    "    yield combined_ppg, combined_ecg, combined_seg_dbp, combined_seg_sbp\n",
    "train_dir = 'C:\\\\Users\\\\nihal\\\\Desktop\\\\NIHAL_IMP_DOCS\\\\Internship_PPG\\\\Train_data'\n",
    "val_dir = 'C:\\\\Users\\\\nihal\\\\Desktop\\\\NIHAL_IMP_DOCS\\\\Internship_PPG\\\\Validation_data'\n",
    "test_dir = 'C:\\\\Users\\\\nihal\\\\Desktop\\\\NIHAL_IMP_DOCS\\\\Internship_PPG\\\\Test_data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0baa9d13-e7d2-45a1-90d0-0d2d3102c91b",
   "metadata": {},
   "outputs": [],
   "source": [
    "val_data_generator = combine_data_from_folder(val_dir, batch_size=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a51447d6-3450-43e3-993b-38fcdfa0bf33",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.manifold import TSNE\n",
    "from sklearn.decomposition import PCA\n",
    "import os\n",
    "\n",
    "vg_image_dir = 'Val_VG'\n",
    "\n",
    "\n",
    "def load_vg_images(batch_idx):\n",
    "    vg_images_path = os.path.join(vg_image_dir, f'Val_VG_batch_{batch_idx}.npz')\n",
    "    \n",
    "    if not os.path.exists(vg_images_path) :\n",
    "        print(f\"VG image files do not exist for Batch {batch_idx}.\")\n",
    "        return None\n",
    "    \n",
    "    vg_images = np.load(vg_images_path)['vg_images']\n",
    "\n",
    "    \n",
    "    return vg_images\n",
    "\n",
    "def plot_vg_images(vg_images, num_images=5, image_size=(224, 224)):\n",
    "    for i in range(min(num_images, vg_images.shape[0])):\n",
    "        plt.figure(figsize=(5, 5))\n",
    "        # Reshape each VG image to its original 2D shape\n",
    "        img = vg_images[i].reshape(image_size)\n",
    "        plt.imshow(img)\n",
    "        plt.title(f'VG Image {i+1}')\n",
    "        plt.axis('off')\n",
    "        plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e60dfb89-939a-4016-84d3-8f7a6d34428e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing Batch 1...\n",
      "Batch 1 processing complete.\n",
      "Processing Batch 2...\n",
      "Batch 2 processing complete.\n",
      "Processing Batch 3...\n",
      "Batch 3 processing complete.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "all_vg_images = []\n",
    "all_sbp_values = []\n",
    "all_dbp_values = []\n",
    "\n",
    "\n",
    "for batch_idx, (combined_ppg_batch, combined_ecg_batch, combined_seg_dbp_batch, combined_seg_sbp_batch) in enumerate(val_data_generator):\n",
    "   \n",
    "    \n",
    "    if batch_idx + 1>3:\n",
    "        break\n",
    "    print(f\"Processing Batch {batch_idx + 1}...\")\n",
    "    vg_images = load_vg_images(batch_idx + 1)\n",
    "    if vg_images is None:\n",
    "        print(f\"No VG images found for Batch {batch_idx + 1}. Stopping...\")\n",
    "        break\n",
    "    \n",
    "    flattened_vg_images = np.array(vg_images)\n",
    "    \n",
    "    all_vg_images.append(flattened_vg_images)\n",
    "    all_sbp_values.append(np.array(combined_seg_sbp_batch.flatten()))\n",
    "    all_dbp_values.append(np.array(combined_seg_dbp_batch.flatten()))\n",
    "\n",
    "    print(f\"Batch {batch_idx + 1} processing complete.\")\n",
    "    \n",
    "\n",
    "all_vg_images = np.concatenate(all_vg_images, axis=0)\n",
    "all_sbp_values = np.concatenate(all_sbp_values, axis=0)\n",
    "all_dbp_values = np.concatenate(all_dbp_values, axis=0)     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1e1d72af-f96a-4f39-afdb-7019ae0b6b25",
   "metadata": {},
   "outputs": [],
   "source": [
    "y = np.column_stack((all_sbp_values, all_dbp_values))\n",
    "vg_images_normal = np.array(all_vg_images)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b9b58543-d8bc-46a6-a6fc-753173bbab4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14850"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(vg_images_normal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "20b416dd-01d6-4200-af03-0c8fa28814e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14850"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(y)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cae92c2e-f941-4a67-8051-ceb88e417b8d",
   "metadata": {},
   "source": [
    "# VGG19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d63d7721-2b29-427a-b9fc-0399ee0158d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from tensorflow.keras.applications import VGG19\n",
    "from tensorflow.keras.applications.vgg19 import preprocess_input\n",
    "from tensorflow.keras.models import Model\n",
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "from sklearn.linear_model import Ridge\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dbf853d9-5e7c-4d74-ab3d-ab3ff798d157",
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Flatten, Dropout\n",
    "from keras.optimizers import Adam\n",
    "from keras.utils import to_categorical\n",
    "\n",
    "# Define the batch size\n",
    "batch_size = 32\n",
    "\n",
    "# Function to generate image batches\n",
    "def image_generator(images, labels, batch_size):\n",
    "    num_samples = images.shape[0]\n",
    "    while True:\n",
    "        for offset in range(0, num_samples, batch_size):\n",
    "            batch_images = images[offset:offset+batch_size]\n",
    "            batch_labels = labels[offset:offset+batch_size]\n",
    "            batch_images_reshaped = batch_images.reshape(-1, 224, 224, 1)\n",
    "            batch_images_rgb = np.concatenate([batch_images_reshaped] * 3, axis=-1)\n",
    "            batch_images_preprocessed = preprocess_input(batch_images_rgb)\n",
    "            yield batch_images_preprocessed, batch_labels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "20b9d98c-0c2e-4d7d-9106-d186f02e0c10",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"model\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " input_1 (InputLayer)        [(None, 224, 224, 3)]     0         \n",
      "                                                                 \n",
      " block1_conv1 (Conv2D)       (None, 224, 224, 64)      1792      \n",
      "                                                                 \n",
      " block1_conv2 (Conv2D)       (None, 224, 224, 64)      36928     \n",
      "                                                                 \n",
      " block1_pool (MaxPooling2D)  (None, 112, 112, 64)      0         \n",
      "                                                                 \n",
      " block2_conv1 (Conv2D)       (None, 112, 112, 128)     73856     \n",
      "                                                                 \n",
      " block2_conv2 (Conv2D)       (None, 112, 112, 128)     147584    \n",
      "                                                                 \n",
      " block2_pool (MaxPooling2D)  (None, 56, 56, 128)       0         \n",
      "                                                                 \n",
      " block3_conv1 (Conv2D)       (None, 56, 56, 256)       295168    \n",
      "                                                                 \n",
      " block3_conv2 (Conv2D)       (None, 56, 56, 256)       590080    \n",
      "                                                                 \n",
      " block3_conv3 (Conv2D)       (None, 56, 56, 256)       590080    \n",
      "                                                                 \n",
      " block3_conv4 (Conv2D)       (None, 56, 56, 256)       590080    \n",
      "                                                                 \n",
      " block3_pool (MaxPooling2D)  (None, 28, 28, 256)       0         \n",
      "                                                                 \n",
      " block4_conv1 (Conv2D)       (None, 28, 28, 512)       1180160   \n",
      "                                                                 \n",
      " block4_conv2 (Conv2D)       (None, 28, 28, 512)       2359808   \n",
      "                                                                 \n",
      " block4_conv3 (Conv2D)       (None, 28, 28, 512)       2359808   \n",
      "                                                                 \n",
      " block4_conv4 (Conv2D)       (None, 28, 28, 512)       2359808   \n",
      "                                                                 \n",
      " block4_pool (MaxPooling2D)  (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " block5_conv1 (Conv2D)       (None, 14, 14, 512)       2359808   \n",
      "                                                                 \n",
      " block5_conv2 (Conv2D)       (None, 14, 14, 512)       2359808   \n",
      "                                                                 \n",
      " block5_conv3 (Conv2D)       (None, 14, 14, 512)       2359808   \n",
      "                                                                 \n",
      " block5_conv4 (Conv2D)       (None, 14, 14, 512)       2359808   \n",
      "                                                                 \n",
      " block5_pool (MaxPooling2D)  (None, 7, 7, 512)         0         \n",
      "                                                                 \n",
      " flatten (Flatten)           (None, 25088)             0         \n",
      "                                                                 \n",
      " fc1 (Dense)                 (None, 512)               12845568  \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 32869952 (125.39 MB)\n",
      "Trainable params: 12845568 (49.00 MB)\n",
      "Non-trainable params: 20024384 (76.39 MB)\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "base_model = VGG19(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n",
    "\n",
    "\n",
    "for layer in base_model.layers:\n",
    "    layer.trainable = False\n",
    "\n",
    "\n",
    "x = base_model.output\n",
    "x = Flatten(name='flatten')(x)\n",
    "#x = Dense(4096, activation='relu', name='fc1')(x)\n",
    "#x = Dense(4096, activation='relu', name='fc2')(x)\n",
    "output = Dense(512, activation='linear',name='fc1')(x)  \n",
    "\n",
    "\n",
    "model = Model(inputs=base_model.input, outputs=output)\n",
    "\n",
    "\n",
    "model.summary()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "7f1889f9-fdc2-445d-b316-6e7cc453e9e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "images = vg_images_normal\n",
    "labels = y\n",
    "num_samples = images.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e855c7ad-0d75-4a9f-bd2a-09dca579cd3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define a new model to extract features from the 'fc1' layer of the trained model\n",
    "feature_extractor = Model(inputs=model.input, outputs=model.get_layer('fc1').output)\n",
    "\n",
    "# Function to extract features\n",
    "def extract_features(generator, num_samples):\n",
    "    features = []\n",
    "    for batch_images, _ in generator:\n",
    "        batch_features = feature_extractor.predict(batch_images)\n",
    "        features.append(batch_features)\n",
    "        if len(features) * batch_size >= num_samples:\n",
    "            break\n",
    "    return np.vstack(features)\n",
    "\n",
    "\n",
    "feature_generator_normal = image_generator(images, np.zeros((num_samples,)), batch_size)\n",
    "\n",
    "\n",
    "features_normal = extract_features(feature_generator_normal, num_samples)\n",
    "\n",
    "\n",
    "features_normal_flattened= features_normal.reshape(features_normal.shape[0],-1)\n",
    "\n",
    "\n",
    "features= features_normal_flattened"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b298dfb5-8f78-49d9-b003-654189233878",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "vg_features_train,vg_features_test,label_vgg_train,label_vgg_test=train_test_split(features,labels, test_size=0.2, random_state=42)\n",
    "ridge_model = Ridge(alpha=1.0)  \n",
    "ridge_model.fit(vg_features_train, label_vgg_train)\n",
    "\n",
    "\n",
    "predicted_values_split = ridge_model.predict(vg_features_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee0e11d0-2b69-4377-8871-7e0a3fd6db0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "predicted_sbp =predicted_values_split[:, 0]  # Assuming the first column is SBP\n",
    "predicted_dbp =predicted_values_split[:, 1]  # Assuming the second column is DBP\n",
    "\n",
    "true_sbp = label_vgg_test[:, 0]  # True SBP values from q\n",
    "true_dbp = label_vgg_test[:, 1]  # True DBP values from q\n",
    "\n",
    "def calculate_metrics(predicted, true):\n",
    "    # Calculate R (correlation coefficient)\n",
    "    R = np.corrcoef(predicted, true)[0, 1]\n",
    "\n",
    "    # Calculate MAE (Mean Absolute Error)\n",
    "    MAE = np.mean(np.abs(predicted - true))\n",
    "\n",
    "    # Calculate RMSE (Root Mean Squared Error)\n",
    "    RMSE = np.sqrt(np.mean((predicted - true) ** 2))\n",
    "\n",
    "    # Calculate ME ± SD (Mean Error ± Standard Deviation)\n",
    "    ME = np.mean(predicted - true)\n",
    "    SD = np.std(predicted - true)\n",
    "\n",
    "    return R, MAE, RMSE, (ME, SD)\n",
    "\n",
    "# Calculate metrics for SBP\n",
    "sbp_metrics = calculate_metrics(predicted_sbp, true_sbp)\n",
    "print(f\"SBP Metrics: R={sbp_metrics[0]}, MAE={sbp_metrics[1]}, RMSE={sbp_metrics[2]}, ME±SD={sbp_metrics[3]}\")\n",
    "\n",
    "# Calculate metrics for DBP\n",
    "dbp_metrics = calculate_metrics(predicted_dbp, true_dbp)\n",
    "print(f\"DBP Metrics: R={dbp_metrics[0]}, MAE={dbp_metrics[1]}, RMSE={dbp_metrics[2]}, ME±SD={dbp_metrics[3]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "283cea53-cf8d-46e2-bfbb-796f8b53126a",
   "metadata": {},
   "outputs": [],
   "source": [
    "num_samples"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ded45a1-d00c-4c55-bf73-4ed394ca3e77",
   "metadata": {},
   "source": [
    "# MOBILE NET"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ae48f01d-3732-42d9-92b0-6285237d276c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Flatten, Dropout\n",
    "from keras.optimizers import Adam\n",
    "from keras.utils import to_categorical\n",
    "from tensorflow.keras.preprocessing.image import ImageDataGenerator\n",
    "from sklearn.linear_model import Ridge\n",
    "\n",
    "# Define the batch size\n",
    "batch_size = 32\n",
    "\n",
    "# Function to generate image batches\n",
    "def image_generator(images, labels, batch_size):\n",
    "    num_samples = images.shape[0]\n",
    "    while True:\n",
    "        for offset in range(0, num_samples, batch_size):\n",
    "            batch_images = images[offset:offset+batch_size]\n",
    "            batch_labels = labels[offset:offset+batch_size]\n",
    "            batch_images_reshaped = batch_images.reshape(-1, 224, 224, 1)\n",
    "            batch_images_rgb = np.concatenate([batch_images_reshaped] * 3, axis=-1)\n",
    "            batch_images_preprocessed = preprocess_input(batch_images_rgb)\n",
    "            yield batch_images_preprocessed, batch_labels\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a00feed3-d9cb-4ac8-981e-319839059514",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"model\"\n",
      "_________________________________________________________________\n",
      " Layer (type)                Output Shape              Param #   \n",
      "=================================================================\n",
      " input_1 (InputLayer)        [(None, 224, 224, 3)]     0         \n",
      "                                                                 \n",
      " conv1 (Conv2D)              (None, 112, 112, 32)      864       \n",
      "                                                                 \n",
      " conv1_bn (BatchNormalizati  (None, 112, 112, 32)      128       \n",
      " on)                                                             \n",
      "                                                                 \n",
      " conv1_relu (ReLU)           (None, 112, 112, 32)      0         \n",
      "                                                                 \n",
      " conv_dw_1 (DepthwiseConv2D  (None, 112, 112, 32)      288       \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_1_bn (BatchNormali  (None, 112, 112, 32)      128       \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_1_relu (ReLU)       (None, 112, 112, 32)      0         \n",
      "                                                                 \n",
      " conv_pw_1 (Conv2D)          (None, 112, 112, 64)      2048      \n",
      "                                                                 \n",
      " conv_pw_1_bn (BatchNormali  (None, 112, 112, 64)      256       \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_1_relu (ReLU)       (None, 112, 112, 64)      0         \n",
      "                                                                 \n",
      " conv_pad_2 (ZeroPadding2D)  (None, 113, 113, 64)      0         \n",
      "                                                                 \n",
      " conv_dw_2 (DepthwiseConv2D  (None, 56, 56, 64)        576       \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_2_bn (BatchNormali  (None, 56, 56, 64)        256       \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_2_relu (ReLU)       (None, 56, 56, 64)        0         \n",
      "                                                                 \n",
      " conv_pw_2 (Conv2D)          (None, 56, 56, 128)       8192      \n",
      "                                                                 \n",
      " conv_pw_2_bn (BatchNormali  (None, 56, 56, 128)       512       \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_2_relu (ReLU)       (None, 56, 56, 128)       0         \n",
      "                                                                 \n",
      " conv_dw_3 (DepthwiseConv2D  (None, 56, 56, 128)       1152      \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_3_bn (BatchNormali  (None, 56, 56, 128)       512       \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_3_relu (ReLU)       (None, 56, 56, 128)       0         \n",
      "                                                                 \n",
      " conv_pw_3 (Conv2D)          (None, 56, 56, 128)       16384     \n",
      "                                                                 \n",
      " conv_pw_3_bn (BatchNormali  (None, 56, 56, 128)       512       \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_3_relu (ReLU)       (None, 56, 56, 128)       0         \n",
      "                                                                 \n",
      " conv_pad_4 (ZeroPadding2D)  (None, 57, 57, 128)       0         \n",
      "                                                                 \n",
      " conv_dw_4 (DepthwiseConv2D  (None, 28, 28, 128)       1152      \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_4_bn (BatchNormali  (None, 28, 28, 128)       512       \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_4_relu (ReLU)       (None, 28, 28, 128)       0         \n",
      "                                                                 \n",
      " conv_pw_4 (Conv2D)          (None, 28, 28, 256)       32768     \n",
      "                                                                 \n",
      " conv_pw_4_bn (BatchNormali  (None, 28, 28, 256)       1024      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_4_relu (ReLU)       (None, 28, 28, 256)       0         \n",
      "                                                                 \n",
      " conv_dw_5 (DepthwiseConv2D  (None, 28, 28, 256)       2304      \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_5_bn (BatchNormali  (None, 28, 28, 256)       1024      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_5_relu (ReLU)       (None, 28, 28, 256)       0         \n",
      "                                                                 \n",
      " conv_pw_5 (Conv2D)          (None, 28, 28, 256)       65536     \n",
      "                                                                 \n",
      " conv_pw_5_bn (BatchNormali  (None, 28, 28, 256)       1024      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_5_relu (ReLU)       (None, 28, 28, 256)       0         \n",
      "                                                                 \n",
      " conv_pad_6 (ZeroPadding2D)  (None, 29, 29, 256)       0         \n",
      "                                                                 \n",
      " conv_dw_6 (DepthwiseConv2D  (None, 14, 14, 256)       2304      \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_6_bn (BatchNormali  (None, 14, 14, 256)       1024      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_6_relu (ReLU)       (None, 14, 14, 256)       0         \n",
      "                                                                 \n",
      " conv_pw_6 (Conv2D)          (None, 14, 14, 512)       131072    \n",
      "                                                                 \n",
      " conv_pw_6_bn (BatchNormali  (None, 14, 14, 512)       2048      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_6_relu (ReLU)       (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_dw_7 (DepthwiseConv2D  (None, 14, 14, 512)       4608      \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_7_bn (BatchNormali  (None, 14, 14, 512)       2048      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_7_relu (ReLU)       (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_pw_7 (Conv2D)          (None, 14, 14, 512)       262144    \n",
      "                                                                 \n",
      " conv_pw_7_bn (BatchNormali  (None, 14, 14, 512)       2048      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_7_relu (ReLU)       (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_dw_8 (DepthwiseConv2D  (None, 14, 14, 512)       4608      \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_8_bn (BatchNormali  (None, 14, 14, 512)       2048      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_8_relu (ReLU)       (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_pw_8 (Conv2D)          (None, 14, 14, 512)       262144    \n",
      "                                                                 \n",
      " conv_pw_8_bn (BatchNormali  (None, 14, 14, 512)       2048      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_8_relu (ReLU)       (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_dw_9 (DepthwiseConv2D  (None, 14, 14, 512)       4608      \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_9_bn (BatchNormali  (None, 14, 14, 512)       2048      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_dw_9_relu (ReLU)       (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_pw_9 (Conv2D)          (None, 14, 14, 512)       262144    \n",
      "                                                                 \n",
      " conv_pw_9_bn (BatchNormali  (None, 14, 14, 512)       2048      \n",
      " zation)                                                         \n",
      "                                                                 \n",
      " conv_pw_9_relu (ReLU)       (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_dw_10 (DepthwiseConv2  (None, 14, 14, 512)       4608      \n",
      " D)                                                              \n",
      "                                                                 \n",
      " conv_dw_10_bn (BatchNormal  (None, 14, 14, 512)       2048      \n",
      " ization)                                                        \n",
      "                                                                 \n",
      " conv_dw_10_relu (ReLU)      (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_pw_10 (Conv2D)         (None, 14, 14, 512)       262144    \n",
      "                                                                 \n",
      " conv_pw_10_bn (BatchNormal  (None, 14, 14, 512)       2048      \n",
      " ization)                                                        \n",
      "                                                                 \n",
      " conv_pw_10_relu (ReLU)      (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_dw_11 (DepthwiseConv2  (None, 14, 14, 512)       4608      \n",
      " D)                                                              \n",
      "                                                                 \n",
      " conv_dw_11_bn (BatchNormal  (None, 14, 14, 512)       2048      \n",
      " ization)                                                        \n",
      "                                                                 \n",
      " conv_dw_11_relu (ReLU)      (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_pw_11 (Conv2D)         (None, 14, 14, 512)       262144    \n",
      "                                                                 \n",
      " conv_pw_11_bn (BatchNormal  (None, 14, 14, 512)       2048      \n",
      " ization)                                                        \n",
      "                                                                 \n",
      " conv_pw_11_relu (ReLU)      (None, 14, 14, 512)       0         \n",
      "                                                                 \n",
      " conv_pad_12 (ZeroPadding2D  (None, 15, 15, 512)       0         \n",
      " )                                                               \n",
      "                                                                 \n",
      " conv_dw_12 (DepthwiseConv2  (None, 7, 7, 512)         4608      \n",
      " D)                                                              \n",
      "                                                                 \n",
      " conv_dw_12_bn (BatchNormal  (None, 7, 7, 512)         2048      \n",
      " ization)                                                        \n",
      "                                                                 \n",
      " conv_dw_12_relu (ReLU)      (None, 7, 7, 512)         0         \n",
      "                                                                 \n",
      " conv_pw_12 (Conv2D)         (None, 7, 7, 1024)        524288    \n",
      "                                                                 \n",
      " conv_pw_12_bn (BatchNormal  (None, 7, 7, 1024)        4096      \n",
      " ization)                                                        \n",
      "                                                                 \n",
      " conv_pw_12_relu (ReLU)      (None, 7, 7, 1024)        0         \n",
      "                                                                 \n",
      " conv_dw_13 (DepthwiseConv2  (None, 7, 7, 1024)        9216      \n",
      " D)                                                              \n",
      "                                                                 \n",
      " conv_dw_13_bn (BatchNormal  (None, 7, 7, 1024)        4096      \n",
      " ization)                                                        \n",
      "                                                                 \n",
      " conv_dw_13_relu (ReLU)      (None, 7, 7, 1024)        0         \n",
      "                                                                 \n",
      " conv_pw_13 (Conv2D)         (None, 7, 7, 1024)        1048576   \n",
      "                                                                 \n",
      " conv_pw_13_bn (BatchNormal  (None, 7, 7, 1024)        4096      \n",
      " ization)                                                        \n",
      "                                                                 \n",
      " conv_pw_13_relu (ReLU)      (None, 7, 7, 1024)        0         \n",
      "                                                                 \n",
      " flatten (Flatten)           (None, 50176)             0         \n",
      "                                                                 \n",
      " fc1 (Dense)                 (None, 512)               25690624  \n",
      "                                                                 \n",
      "=================================================================\n",
      "Total params: 28919488 (110.32 MB)\n",
      "Trainable params: 25690624 (98.00 MB)\n",
      "Non-trainable params: 3228864 (12.32 MB)\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.keras.applications import MobileNet\n",
    "from tensorflow.keras.layers import Flatten, Dense\n",
    "from tensorflow.keras.models import Model\n",
    "from tensorflow.keras.applications.mobilenet import preprocess_input\n",
    "\n",
    "\n",
    "base_model_mobile = MobileNet(weights='imagenet', include_top=False, input_shape=(224, 224, 3))\n",
    "\n",
    "\n",
    "for layer in base_model_mobile.layers:\n",
    "    layer.trainable = False\n",
    "\n",
    "\n",
    "x = base_model_mobile.output\n",
    "x = Flatten(name='flatten')(x)\n",
    "output = Dense(512, activation='linear', name='fc1')(x)\n",
    "\n",
    "\n",
    "model_mobile = Model(inputs=base_model_mobile.input, outputs=output)\n",
    "\n",
    "model_mobile.summary()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4b337ca6-3a35-436c-b50e-660eb459426a",
   "metadata": {},
   "outputs": [],
   "source": [
    "images = vg_images_normal\n",
    "labels = y\n",
    "num_samples = images.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8241becd-09f4-4517-ad5c-8139a1e438a3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1/1 [==============================] - 1s 1s/step\n",
      "1/1 [==============================] - 0s 411ms/step\n",
      "1/1 [==============================] - 0s 383ms/step\n",
      "1/1 [==============================] - 0s 388ms/step\n",
      "1/1 [==============================] - 0s 394ms/step\n",
      "1/1 [==============================] - 0s 353ms/step\n",
      "1/1 [==============================] - 0s 409ms/step\n",
      "1/1 [==============================] - 0s 387ms/step\n",
      "1/1 [==============================] - 0s 406ms/step\n",
      "1/1 [==============================] - 0s 385ms/step\n",
      "1/1 [==============================] - 0s 390ms/step\n",
      "1/1 [==============================] - 0s 386ms/step\n",
      "1/1 [==============================] - 0s 372ms/step\n",
      "1/1 [==============================] - 0s 381ms/step\n",
      "1/1 [==============================] - 0s 346ms/step\n",
      "1/1 [==============================] - 0s 359ms/step\n",
      "1/1 [==============================] - 0s 358ms/step\n",
      "1/1 [==============================] - 0s 390ms/step\n",
      "1/1 [==============================] - 0s 423ms/step\n",
      "1/1 [==============================] - 0s 451ms/step\n",
      "1/1 [==============================] - 1s 570ms/step\n",
      "1/1 [==============================] - 1s 599ms/step\n",
      "1/1 [==============================] - 1s 589ms/step\n",
      "1/1 [==============================] - 1s 540ms/step\n",
      "1/1 [==============================] - 1s 544ms/step\n",
      "1/1 [==============================] - 1s 580ms/step\n",
      "1/1 [==============================] - 1s 534ms/step\n",
      "1/1 [==============================] - 1s 529ms/step\n",
      "1/1 [==============================] - 1s 550ms/step\n",
      "1/1 [==============================] - 1s 519ms/step\n",
      "1/1 [==============================] - 1s 529ms/step\n",
      "1/1 [==============================] - 0s 448ms/step\n",
      "1/1 [==============================] - 0s 432ms/step\n",
      "1/1 [==============================] - 0s 423ms/step\n",
      "1/1 [==============================] - 0s 422ms/step\n",
      "1/1 [==============================] - 0s 427ms/step\n",
      "1/1 [==============================] - 0s 432ms/step\n",
      "1/1 [==============================] - 0s 430ms/step\n",
      "1/1 [==============================] - 0s 426ms/step\n",
      "1/1 [==============================] - 0s 449ms/step\n",
      "1/1 [==============================] - 0s 469ms/step\n",
      "1/1 [==============================] - 0s 426ms/step\n",
      "1/1 [==============================] - 0s 426ms/step\n",
      "1/1 [==============================] - 0s 434ms/step\n",
      "1/1 [==============================] - 0s 447ms/step\n",
      "1/1 [==============================] - 0s 455ms/step\n",
      "1/1 [==============================] - 0s 451ms/step\n",
      "1/1 [==============================] - 1s 510ms/step\n",
      "1/1 [==============================] - 1s 508ms/step\n",
      "1/1 [==============================] - 0s 498ms/step\n",
      "1/1 [==============================] - 0s 492ms/step\n",
      "1/1 [==============================] - 0s 479ms/step\n",
      "1/1 [==============================] - 0s 480ms/step\n",
      "1/1 [==============================] - 0s 495ms/step\n",
      "1/1 [==============================] - 1s 525ms/step\n",
      "1/1 [==============================] - 1s 523ms/step\n",
      "1/1 [==============================] - 1s 520ms/step\n",
      "1/1 [==============================] - 1s 556ms/step\n",
      "1/1 [==============================] - 1s 528ms/step\n",
      "1/1 [==============================] - 1s 551ms/step\n",
      "1/1 [==============================] - 1s 621ms/step\n",
      "1/1 [==============================] - 1s 536ms/step\n",
      "1/1 [==============================] - 0s 477ms/step\n",
      "1/1 [==============================] - 0s 447ms/step\n",
      "1/1 [==============================] - 0s 452ms/step\n",
      "1/1 [==============================] - 0s 478ms/step\n",
      "1/1 [==============================] - 0s 446ms/step\n",
      "1/1 [==============================] - 0s 476ms/step\n",
      "1/1 [==============================] - 0s 421ms/step\n",
      "1/1 [==============================] - 0s 446ms/step\n",
      "1/1 [==============================] - 0s 422ms/step\n",
      "1/1 [==============================] - 0s 426ms/step\n",
      "1/1 [==============================] - 0s 454ms/step\n",
      "1/1 [==============================] - 0s 468ms/step\n",
      "1/1 [==============================] - 0s 493ms/step\n",
      "1/1 [==============================] - 0s 490ms/step\n",
      "1/1 [==============================] - 1s 563ms/step\n",
      "1/1 [==============================] - 1s 505ms/step\n",
      "1/1 [==============================] - 1s 539ms/step\n",
      "1/1 [==============================] - 1s 518ms/step\n",
      "1/1 [==============================] - 1s 540ms/step\n",
      "1/1 [==============================] - 1s 621ms/step\n",
      "1/1 [==============================] - 1s 568ms/step\n",
      "1/1 [==============================] - 1s 627ms/step\n",
      "1/1 [==============================] - 1s 590ms/step\n",
      "1/1 [==============================] - 1s 552ms/step\n",
      "1/1 [==============================] - 1s 532ms/step\n",
      "1/1 [==============================] - 1s 586ms/step\n",
      "1/1 [==============================] - 1s 509ms/step\n",
      "1/1 [==============================] - 1s 505ms/step\n",
      "1/1 [==============================] - 0s 481ms/step\n",
      "1/1 [==============================] - 0s 489ms/step\n",
      "1/1 [==============================] - 0s 447ms/step\n",
      "1/1 [==============================] - 1s 502ms/step\n",
      "1/1 [==============================] - 1s 593ms/step\n",
      "1/1 [==============================] - 0s 481ms/step\n",
      "1/1 [==============================] - 0s 486ms/step\n",
      "1/1 [==============================] - 0s 464ms/step\n",
      "1/1 [==============================] - 0s 476ms/step\n",
      "1/1 [==============================] - 0s 448ms/step\n",
      "1/1 [==============================] - 0s 488ms/step\n",
      "1/1 [==============================] - 0s 459ms/step\n",
      "1/1 [==============================] - 0s 452ms/step\n",
      "1/1 [==============================] - 1s 518ms/step\n",
      "1/1 [==============================] - 1s 575ms/step\n",
      "1/1 [==============================] - 0s 490ms/step\n",
      "1/1 [==============================] - 0s 495ms/step\n",
      "1/1 [==============================] - 1s 508ms/step\n",
      "1/1 [==============================] - 0s 482ms/step\n",
      "1/1 [==============================] - 1s 522ms/step\n",
      "1/1 [==============================] - 1s 510ms/step\n",
      "1/1 [==============================] - 1s 527ms/step\n",
      "1/1 [==============================] - 1s 532ms/step\n",
      "1/1 [==============================] - 0s 487ms/step\n",
      "1/1 [==============================] - 0s 474ms/step\n",
      "1/1 [==============================] - 1s 509ms/step\n",
      "1/1 [==============================] - 1s 505ms/step\n",
      "1/1 [==============================] - 0s 437ms/step\n",
      "1/1 [==============================] - 0s 453ms/step\n",
      "1/1 [==============================] - 0s 442ms/step\n",
      "1/1 [==============================] - 0s 453ms/step\n",
      "1/1 [==============================] - 1s 527ms/step\n",
      "1/1 [==============================] - 0s 458ms/step\n",
      "1/1 [==============================] - 0s 438ms/step\n",
      "1/1 [==============================] - 1s 540ms/step\n",
      "1/1 [==============================] - 1s 523ms/step\n",
      "1/1 [==============================] - 0s 453ms/step\n",
      "1/1 [==============================] - 1s 504ms/step\n",
      "1/1 [==============================] - 1s 598ms/step\n",
      "1/1 [==============================] - 0s 488ms/step\n",
      "1/1 [==============================] - 0s 451ms/step\n",
      "1/1 [==============================] - 0s 456ms/step\n",
      "1/1 [==============================] - 1s 507ms/step\n",
      "1/1 [==============================] - 0s 480ms/step\n",
      "1/1 [==============================] - 1s 506ms/step\n",
      "1/1 [==============================] - 1s 517ms/step\n",
      "1/1 [==============================] - 1s 511ms/step\n",
      "1/1 [==============================] - 1s 523ms/step\n",
      "1/1 [==============================] - 1s 506ms/step\n",
      "1/1 [==============================] - 1s 603ms/step\n",
      "1/1 [==============================] - 1s 523ms/step\n",
      "1/1 [==============================] - 1s 510ms/step\n",
      "1/1 [==============================] - 1s 502ms/step\n",
      "1/1 [==============================] - 1s 537ms/step\n",
      "1/1 [==============================] - 0s 470ms/step\n",
      "1/1 [==============================] - 0s 464ms/step\n",
      "1/1 [==============================] - 1s 585ms/step\n",
      "1/1 [==============================] - 1s 780ms/step\n",
      "1/1 [==============================] - 1s 687ms/step\n",
      "1/1 [==============================] - 1s 620ms/step\n",
      "1/1 [==============================] - 1s 626ms/step\n",
      "1/1 [==============================] - 1s 562ms/step\n",
      "1/1 [==============================] - 1s 623ms/step\n",
      "1/1 [==============================] - 1s 644ms/step\n",
      "1/1 [==============================] - 1s 785ms/step\n",
      "1/1 [==============================] - 1s 875ms/step\n",
      "1/1 [==============================] - 1s 846ms/step\n",
      "1/1 [==============================] - 1s 694ms/step\n",
      "1/1 [==============================] - 1s 580ms/step\n",
      "1/1 [==============================] - 1s 640ms/step\n",
      "1/1 [==============================] - 1s 650ms/step\n",
      "1/1 [==============================] - 1s 610ms/step\n",
      "1/1 [==============================] - 1s 688ms/step\n",
      "1/1 [==============================] - 1s 523ms/step\n",
      "1/1 [==============================] - 1s 756ms/step\n",
      "1/1 [==============================] - 1s 783ms/step\n",
      "1/1 [==============================] - 1s 691ms/step\n",
      "1/1 [==============================] - 1s 501ms/step\n",
      "1/1 [==============================] - 1s 547ms/step\n",
      "1/1 [==============================] - 1s 580ms/step\n",
      "1/1 [==============================] - 1s 528ms/step\n",
      "1/1 [==============================] - 1s 512ms/step\n",
      "1/1 [==============================] - 0s 473ms/step\n",
      "1/1 [==============================] - 1s 520ms/step\n",
      "1/1 [==============================] - 1s 566ms/step\n",
      "1/1 [==============================] - 1s 544ms/step\n",
      "1/1 [==============================] - 0s 471ms/step\n",
      "1/1 [==============================] - 1s 527ms/step\n",
      "1/1 [==============================] - 1s 512ms/step\n",
      "1/1 [==============================] - 1s 538ms/step\n",
      "1/1 [==============================] - 1s 502ms/step\n",
      "1/1 [==============================] - 1s 535ms/step\n",
      "1/1 [==============================] - 1s 566ms/step\n",
      "1/1 [==============================] - 1s 618ms/step\n",
      "1/1 [==============================] - 1s 533ms/step\n",
      "1/1 [==============================] - 1s 522ms/step\n",
      "1/1 [==============================] - 0s 466ms/step\n",
      "1/1 [==============================] - 1s 501ms/step\n",
      "1/1 [==============================] - 1s 573ms/step\n",
      "1/1 [==============================] - 1s 520ms/step\n",
      "1/1 [==============================] - 0s 464ms/step\n",
      "1/1 [==============================] - 0s 455ms/step\n",
      "1/1 [==============================] - 0s 496ms/step\n",
      "1/1 [==============================] - 1s 549ms/step\n",
      "1/1 [==============================] - 0s 483ms/step\n",
      "1/1 [==============================] - 1s 628ms/step\n",
      "1/1 [==============================] - 1s 693ms/step\n",
      "1/1 [==============================] - 1s 662ms/step\n",
      "1/1 [==============================] - 1s 534ms/step\n",
      "1/1 [==============================] - 1s 626ms/step\n",
      "1/1 [==============================] - 1s 601ms/step\n",
      "1/1 [==============================] - 1s 628ms/step\n",
      "1/1 [==============================] - 1s 594ms/step\n",
      "1/1 [==============================] - 1s 567ms/step\n",
      "1/1 [==============================] - 1s 595ms/step\n",
      "1/1 [==============================] - 1s 655ms/step\n",
      "1/1 [==============================] - 1s 656ms/step\n",
      "1/1 [==============================] - 1s 627ms/step\n",
      "1/1 [==============================] - 1s 610ms/step\n",
      "1/1 [==============================] - 1s 655ms/step\n",
      "1/1 [==============================] - 1s 620ms/step\n",
      "1/1 [==============================] - 1s 594ms/step\n",
      "1/1 [==============================] - 1s 638ms/step\n",
      "1/1 [==============================] - 1s 589ms/step\n",
      "1/1 [==============================] - 1s 571ms/step\n",
      "1/1 [==============================] - 1s 547ms/step\n",
      "1/1 [==============================] - 1s 599ms/step\n",
      "1/1 [==============================] - 1s 545ms/step\n",
      "1/1 [==============================] - 0s 486ms/step\n",
      "1/1 [==============================] - 1s 547ms/step\n",
      "1/1 [==============================] - 0s 465ms/step\n",
      "1/1 [==============================] - 0s 463ms/step\n",
      "1/1 [==============================] - 1s 513ms/step\n",
      "1/1 [==============================] - 0s 482ms/step\n",
      "1/1 [==============================] - 1s 556ms/step\n",
      "1/1 [==============================] - 1s 530ms/step\n",
      "1/1 [==============================] - 1s 546ms/step\n",
      "1/1 [==============================] - 1s 669ms/step\n",
      "1/1 [==============================] - 1s 508ms/step\n",
      "1/1 [==============================] - 1s 613ms/step\n",
      "1/1 [==============================] - 1s 584ms/step\n",
      "1/1 [==============================] - 1s 581ms/step\n",
      "1/1 [==============================] - 1s 588ms/step\n",
      "1/1 [==============================] - 1s 558ms/step\n",
      "1/1 [==============================] - 1s 575ms/step\n",
      "1/1 [==============================] - 1s 655ms/step\n",
      "1/1 [==============================] - 1s 580ms/step\n",
      "1/1 [==============================] - 1s 579ms/step\n",
      "1/1 [==============================] - 1s 573ms/step\n",
      "1/1 [==============================] - 1s 608ms/step\n",
      "1/1 [==============================] - 1s 539ms/step\n",
      "1/1 [==============================] - 1s 588ms/step\n",
      "1/1 [==============================] - 1s 527ms/step\n",
      "1/1 [==============================] - 1s 621ms/step\n",
      "1/1 [==============================] - 1s 513ms/step\n",
      "1/1 [==============================] - 0s 466ms/step\n",
      "1/1 [==============================] - 1s 528ms/step\n",
      "1/1 [==============================] - 1s 511ms/step\n",
      "1/1 [==============================] - 1s 513ms/step\n",
      "1/1 [==============================] - 0s 475ms/step\n",
      "1/1 [==============================] - 0s 445ms/step\n",
      "1/1 [==============================] - 1s 540ms/step\n",
      "1/1 [==============================] - 0s 498ms/step\n",
      "1/1 [==============================] - 1s 549ms/step\n",
      "1/1 [==============================] - 1s 522ms/step\n",
      "1/1 [==============================] - 1s 517ms/step\n",
      "1/1 [==============================] - 1s 576ms/step\n",
      "1/1 [==============================] - 1s 784ms/step\n",
      "1/1 [==============================] - 1s 730ms/step\n",
      "1/1 [==============================] - 1s 649ms/step\n",
      "1/1 [==============================] - 1s 635ms/step\n",
      "1/1 [==============================] - 1s 630ms/step\n",
      "1/1 [==============================] - 1s 636ms/step\n",
      "1/1 [==============================] - 1s 604ms/step\n",
      "1/1 [==============================] - 1s 618ms/step\n",
      "1/1 [==============================] - 1s 606ms/step\n",
      "1/1 [==============================] - 1s 545ms/step\n",
      "1/1 [==============================] - 1s 516ms/step\n",
      "1/1 [==============================] - 1s 547ms/step\n",
      "1/1 [==============================] - 1s 552ms/step\n",
      "1/1 [==============================] - 0s 496ms/step\n",
      "1/1 [==============================] - 1s 553ms/step\n",
      "1/1 [==============================] - 1s 557ms/step\n",
      "1/1 [==============================] - 1s 525ms/step\n",
      "1/1 [==============================] - 1s 558ms/step\n",
      "1/1 [==============================] - 1s 543ms/step\n",
      "1/1 [==============================] - 1s 545ms/step\n",
      "1/1 [==============================] - 1s 556ms/step\n",
      "1/1 [==============================] - 1s 560ms/step\n",
      "1/1 [==============================] - 1s 538ms/step\n",
      "1/1 [==============================] - 1s 567ms/step\n",
      "1/1 [==============================] - 1s 538ms/step\n",
      "1/1 [==============================] - 1s 605ms/step\n",
      "1/1 [==============================] - 0s 490ms/step\n",
      "1/1 [==============================] - 1s 516ms/step\n",
      "1/1 [==============================] - 1s 516ms/step\n",
      "1/1 [==============================] - 1s 536ms/step\n",
      "1/1 [==============================] - 0s 485ms/step\n",
      "1/1 [==============================] - 1s 518ms/step\n",
      "1/1 [==============================] - 0s 489ms/step\n",
      "1/1 [==============================] - 0s 441ms/step\n",
      "1/1 [==============================] - 0s 466ms/step\n",
      "1/1 [==============================] - 0s 453ms/step\n",
      "1/1 [==============================] - 0s 445ms/step\n",
      "1/1 [==============================] - 0s 480ms/step\n",
      "1/1 [==============================] - 0s 470ms/step\n",
      "1/1 [==============================] - 0s 468ms/step\n",
      "1/1 [==============================] - 0s 452ms/step\n",
      "1/1 [==============================] - 0s 452ms/step\n",
      "1/1 [==============================] - 0s 452ms/step\n",
      "1/1 [==============================] - 0s 498ms/step\n",
      "1/1 [==============================] - 0s 449ms/step\n",
      "1/1 [==============================] - 0s 442ms/step\n",
      "1/1 [==============================] - 0s 467ms/step\n",
      "1/1 [==============================] - 1s 552ms/step\n",
      "1/1 [==============================] - 1s 526ms/step\n",
      "1/1 [==============================] - 1s 522ms/step\n",
      "1/1 [==============================] - 1s 533ms/step\n",
      "1/1 [==============================] - 1s 513ms/step\n",
      "1/1 [==============================] - 1s 530ms/step\n",
      "1/1 [==============================] - 1s 515ms/step\n",
      "1/1 [==============================] - 1s 541ms/step\n",
      "1/1 [==============================] - 0s 491ms/step\n",
      "1/1 [==============================] - 1s 521ms/step\n",
      "1/1 [==============================] - 0s 492ms/step\n",
      "1/1 [==============================] - 0s 499ms/step\n",
      "1/1 [==============================] - 1s 589ms/step\n",
      "1/1 [==============================] - 0s 485ms/step\n",
      "1/1 [==============================] - 0s 447ms/step\n",
      "1/1 [==============================] - 0s 444ms/step\n",
      "1/1 [==============================] - 0s 450ms/step\n",
      "1/1 [==============================] - 0s 490ms/step\n",
      "1/1 [==============================] - 0s 461ms/step\n",
      "1/1 [==============================] - 0s 435ms/step\n",
      "1/1 [==============================] - 0s 439ms/step\n",
      "1/1 [==============================] - 1s 505ms/step\n",
      "1/1 [==============================] - 1s 596ms/step\n",
      "1/1 [==============================] - 1s 515ms/step\n",
      "1/1 [==============================] - 1s 509ms/step\n",
      "1/1 [==============================] - 0s 485ms/step\n",
      "1/1 [==============================] - 0s 461ms/step\n",
      "1/1 [==============================] - 1s 512ms/step\n",
      "1/1 [==============================] - 1s 507ms/step\n",
      "1/1 [==============================] - 0s 499ms/step\n",
      "1/1 [==============================] - 1s 511ms/step\n",
      "1/1 [==============================] - 1s 581ms/step\n",
      "1/1 [==============================] - 1s 578ms/step\n",
      "1/1 [==============================] - 1s 563ms/step\n",
      "1/1 [==============================] - 1s 514ms/step\n",
      "1/1 [==============================] - 0s 497ms/step\n",
      "1/1 [==============================] - 1s 513ms/step\n",
      "1/1 [==============================] - 1s 511ms/step\n",
      "1/1 [==============================] - 1s 516ms/step\n",
      "1/1 [==============================] - 1s 507ms/step\n",
      "1/1 [==============================] - 1s 547ms/step\n",
      "1/1 [==============================] - 0s 482ms/step\n",
      "1/1 [==============================] - 0s 456ms/step\n",
      "1/1 [==============================] - 0s 461ms/step\n",
      "1/1 [==============================] - 0s 486ms/step\n",
      "1/1 [==============================] - 0s 460ms/step\n",
      "1/1 [==============================] - 0s 490ms/step\n",
      "1/1 [==============================] - 0s 461ms/step\n",
      "1/1 [==============================] - 0s 465ms/step\n",
      "1/1 [==============================] - 0s 467ms/step\n",
      "1/1 [==============================] - 1s 584ms/step\n",
      "1/1 [==============================] - 1s 517ms/step\n",
      "1/1 [==============================] - 0s 449ms/step\n",
      "1/1 [==============================] - 0s 456ms/step\n",
      "1/1 [==============================] - 0s 497ms/step\n",
      "1/1 [==============================] - 1s 525ms/step\n",
      "1/1 [==============================] - 0s 500ms/step\n",
      "1/1 [==============================] - 1s 506ms/step\n",
      "1/1 [==============================] - 1s 502ms/step\n",
      "1/1 [==============================] - 1s 577ms/step\n",
      "1/1 [==============================] - 1s 510ms/step\n",
      "1/1 [==============================] - 1s 520ms/step\n",
      "1/1 [==============================] - 1s 570ms/step\n",
      "1/1 [==============================] - 1s 594ms/step\n",
      "1/1 [==============================] - 1s 576ms/step\n",
      "1/1 [==============================] - 1s 625ms/step\n",
      "1/1 [==============================] - 1s 565ms/step\n",
      "1/1 [==============================] - 1s 665ms/step\n",
      "1/1 [==============================] - 1s 585ms/step\n",
      "1/1 [==============================] - 1s 529ms/step\n",
      "1/1 [==============================] - 1s 513ms/step\n",
      "1/1 [==============================] - 1s 533ms/step\n",
      "1/1 [==============================] - 0s 488ms/step\n",
      "1/1 [==============================] - 1s 507ms/step\n",
      "1/1 [==============================] - 1s 544ms/step\n",
      "1/1 [==============================] - 1s 516ms/step\n",
      "1/1 [==============================] - 1s 518ms/step\n",
      "1/1 [==============================] - 1s 516ms/step\n",
      "1/1 [==============================] - 1s 679ms/step\n",
      "1/1 [==============================] - 1s 555ms/step\n",
      "1/1 [==============================] - 1s 606ms/step\n",
      "1/1 [==============================] - 1s 552ms/step\n",
      "1/1 [==============================] - 1s 547ms/step\n",
      "1/1 [==============================] - 1s 592ms/step\n",
      "1/1 [==============================] - 1s 734ms/step\n",
      "1/1 [==============================] - 1s 693ms/step\n",
      "1/1 [==============================] - 1s 597ms/step\n",
      "1/1 [==============================] - 1s 610ms/step\n",
      "1/1 [==============================] - 1s 539ms/step\n",
      "1/1 [==============================] - 1s 516ms/step\n",
      "1/1 [==============================] - 1s 502ms/step\n",
      "1/1 [==============================] - 1s 515ms/step\n",
      "1/1 [==============================] - 0s 498ms/step\n",
      "1/1 [==============================] - 1s 538ms/step\n",
      "1/1 [==============================] - 0s 447ms/step\n",
      "1/1 [==============================] - 0s 449ms/step\n",
      "1/1 [==============================] - 0s 462ms/step\n",
      "1/1 [==============================] - 0s 443ms/step\n",
      "1/1 [==============================] - 0s 446ms/step\n",
      "1/1 [==============================] - 0s 466ms/step\n",
      "1/1 [==============================] - 0s 494ms/step\n",
      "1/1 [==============================] - 0s 458ms/step\n",
      "1/1 [==============================] - 0s 495ms/step\n",
      "1/1 [==============================] - 0s 462ms/step\n",
      "1/1 [==============================] - 1s 504ms/step\n",
      "1/1 [==============================] - 0s 473ms/step\n",
      "1/1 [==============================] - 0s 459ms/step\n",
      "1/1 [==============================] - 1s 518ms/step\n",
      "1/1 [==============================] - 1s 542ms/step\n",
      "1/1 [==============================] - 1s 557ms/step\n",
      "1/1 [==============================] - 1s 562ms/step\n",
      "1/1 [==============================] - 1s 596ms/step\n",
      "1/1 [==============================] - 1s 635ms/step\n",
      "1/1 [==============================] - 1s 568ms/step\n",
      "1/1 [==============================] - 1s 557ms/step\n",
      "1/1 [==============================] - 1s 584ms/step\n",
      "1/1 [==============================] - 1s 617ms/step\n",
      "1/1 [==============================] - 1s 561ms/step\n",
      "1/1 [==============================] - 1s 545ms/step\n",
      "1/1 [==============================] - 1s 541ms/step\n",
      "1/1 [==============================] - 0s 488ms/step\n",
      "1/1 [==============================] - 0s 470ms/step\n",
      "1/1 [==============================] - 0s 446ms/step\n",
      "1/1 [==============================] - 0s 451ms/step\n",
      "1/1 [==============================] - 0s 469ms/step\n",
      "1/1 [==============================] - 0s 439ms/step\n",
      "1/1 [==============================] - 0s 435ms/step\n",
      "1/1 [==============================] - 0s 456ms/step\n",
      "1/1 [==============================] - 0s 493ms/step\n",
      "1/1 [==============================] - 0s 452ms/step\n",
      "1/1 [==============================] - 0s 438ms/step\n",
      "1/1 [==============================] - 1s 501ms/step\n",
      "1/1 [==============================] - 0s 450ms/step\n",
      "1/1 [==============================] - 0s 434ms/step\n",
      "1/1 [==============================] - 0s 475ms/step\n",
      "1/1 [==============================] - 1s 506ms/step\n",
      "1/1 [==============================] - 1s 522ms/step\n",
      "1/1 [==============================] - 1s 524ms/step\n",
      "1/1 [==============================] - 1s 533ms/step\n",
      "1/1 [==============================] - 1s 527ms/step\n",
      "1/1 [==============================] - 1s 510ms/step\n",
      "1/1 [==============================] - 1s 550ms/step\n",
      "1/1 [==============================] - 1s 534ms/step\n",
      "1/1 [==============================] - 1s 541ms/step\n",
      "1/1 [==============================] - 1s 559ms/step\n",
      "1/1 [==============================] - 1s 544ms/step\n",
      "1/1 [==============================] - 1s 581ms/step\n",
      "1/1 [==============================] - 1s 503ms/step\n",
      "1/1 [==============================] - 1s 502ms/step\n",
      "1/1 [==============================] - 1s 502ms/step\n",
      "1/1 [==============================] - 1s 501ms/step\n",
      "1/1 [==============================] - 0s 491ms/step\n",
      "1/1 [==============================] - 1s 532ms/step\n",
      "1/1 [==============================] - 0s 493ms/step\n",
      "1/1 [==============================] - 0s 479ms/step\n",
      "1/1 [==============================] - 1s 512ms/step\n",
      "1/1 [==============================] - 1s 505ms/step\n",
      "1/1 [==============================] - 1s 510ms/step\n",
      "1/1 [==============================] - 1s 593ms/step\n",
      "1/1 [==============================] - 1s 533ms/step\n",
      "1/1 [==============================] - 1s 771ms/step\n"
     ]
    }
   ],
   "source": [
    "# Define a new model to extract features from the 'fc1' layer of the trained model\n",
    "feature_extractor_mobile = Model(inputs=model_mobile.input, outputs=model_mobile.get_layer('fc1').output)\n",
    "\n",
    "# Function to extract features\n",
    "def extract_features(generator, num_samples):\n",
    "    features = []\n",
    "    for batch_images, _ in generator:\n",
    "        batch_features = feature_extractor_mobile.predict(batch_images)\n",
    "        features.append(batch_features)\n",
    "        if len(features) * batch_size >= num_samples:\n",
    "            break\n",
    "    return np.vstack(features)\n",
    "\n",
    "\n",
    "feature_generator_normal_1 = image_generator(images, np.zeros((num_samples,)), batch_size)\n",
    "\n",
    "\n",
    "features_normal_1 = extract_features(feature_generator_normal_1, num_samples)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "176fb6a7-5a4b-48c6-88ee-f524ae9e45c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "features_normal_flattened_1= features_normal_1.reshape(features_normal_1.shape[0],-1)\n",
    "\n",
    "\n",
    "features_1= features_normal_flattened_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "78fa2c21-d66b-410f-aca8-a2c8ff702df4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "Mobile_train,Mobile_test,label_train,label_test=train_test_split(features_1,labels, test_size=0.2, random_state=42)\n",
    "ridge_model = Ridge(alpha=1.0)  \n",
    "ridge_model.fit(Mobile_train, label_train)\n",
    "\n",
    "\n",
    "predicted_mobile = ridge_model.predict(Mobile_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9c0a90a8-b86c-4a37-bbee-9b565922bab2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SBP Metrics: R=0.5350883479323466, MAE=11.582464008512448, RMSE=15.454890610537387, ME±SD=(-0.2674706525288677, 15.452575941690514)\n",
      "DBP Metrics: R=0.5773094399159031, MAE=6.686779229214582, RMSE=9.007300820469364, ME±SD=(-0.1878863144349513, 9.005341015379491)\n"
     ]
    }
   ],
   "source": [
    "predicted_sbp =predicted_mobile[:, 0]  # Assuming the first column is SBP\n",
    "predicted_dbp =predicted_mobile[:, 1]  # Assuming the second column is DBP\n",
    "\n",
    "true_sbp = label_test[:, 0]  # True SBP values from q\n",
    "true_dbp = label_test[:, 1]  # True DBP values from q\n",
    "\n",
    "def calculate_metrics(predicted, true):\n",
    "    # Calculate R (correlation coefficient)\n",
    "    R = np.corrcoef(predicted, true)[0, 1]\n",
    "\n",
    "    # Calculate MAE (Mean Absolute Error)\n",
    "    MAE = np.mean(np.abs(predicted - true))\n",
    "\n",
    "    # Calculate RMSE (Root Mean Squared Error)\n",
    "    RMSE = np.sqrt(np.mean((predicted - true) ** 2))\n",
    "\n",
    "    # Calculate ME ± SD (Mean Error ± Standard Deviation)\n",
    "    ME = np.mean(predicted - true)\n",
    "    SD = np.std(predicted - true)\n",
    "\n",
    "    return R, MAE, RMSE, (ME, SD)\n",
    "\n",
    "# Calculate metrics for SBP\n",
    "sbp_metrics = calculate_metrics(predicted_sbp, true_sbp)\n",
    "print(f\"SBP Metrics: R={sbp_metrics[0]}, MAE={sbp_metrics[1]}, RMSE={sbp_metrics[2]}, ME±SD={sbp_metrics[3]}\")\n",
    "\n",
    "# Calculate metrics for DBP\n",
    "dbp_metrics = calculate_metrics(predicted_dbp, true_dbp)\n",
    "print(f\"DBP Metrics: R={dbp_metrics[0]}, MAE={dbp_metrics[1]}, RMSE={dbp_metrics[2]}, ME±SD={dbp_metrics[3]}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1576f043-a648-40d4-82b7-7e682eaf2907",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "14850"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_samples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69c62975-52a0-4f25-9b33-889df69598df",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
